import { Body, Controller, Get, HttpCode, HttpStatus, Post, Render } from '@nestjs/common'
import { plainToInstance } from 'class-transformer'

import { MapDTO, MapNearDTO } from './map.dto'
import { MapService } from './map.service'

@Controller('map')
export class MapController {
  constructor(private readonly service: MapService) {}

  @Get()
  @Render('map')
  render() {
    return { title: '添加地图点' }
  }

  @HttpCode(HttpStatus.OK)
  @Post('add')
  add(@Body() { lnglat }: { lnglat: string[] }) {
    const data = lnglat.map(function (item) {
      const [lng, lat, title] = item.split(',')
      return plainToInstance(MapDTO, { lng, lat, title })
    })

    return this.service.save(data)
  }

  @Get('area')
  @Render('map/area')
  area() {
    return { title: '范围查询' }
  }

  @HttpCode(HttpStatus.OK)
  @Post('near')
  near(@Body() data: MapNearDTO) {
    return this.service.near(data.center, data.radius)
  }
}
